package com.android.examplelist;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioGroup;
import android.view.View;

public class SwipeBackDemoActivity extends SwipeBackActivity implements View.OnClickListener {

	/*
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.actionbar);
		
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		
		actionBar.setSubtitle("Inbox"); 
		actionBar.setTitle("Label:important");
	}
	*/
	   private static final int VIBRATE_DURATION = 20;

	    private int[] mBgColors;

	    private static int mBgIndex = 0;

	    private String mKeyTrackingMode;

	    private RadioGroup mTrackingModeGroup;

	    private SwipeBackLayout mSwipeBackLayout;

	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.swipe_back);
	        changeActionBarColor();
	        findViews();
	        mKeyTrackingMode = getString(R.string.key_tracking_mode);
	        mSwipeBackLayout = getSwipeBackLayout();
	        
	        getSwipeBackLayout().setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
	        

	        mTrackingModeGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
	            @Override
	            public void onCheckedChanged(RadioGroup group, int checkedId) {
	                int edgeFlag;
	                switch (checkedId) {
	                    case R.id.mode_left:
	                        edgeFlag = SwipeBackLayout.EDGE_LEFT;
	                        break;
	                    case R.id.mode_right:
	                        edgeFlag = SwipeBackLayout.EDGE_RIGHT;
	                        break;
	                    case R.id.mode_bottom:
	                        edgeFlag = SwipeBackLayout.EDGE_BOTTOM;
	                        break;
	                    default:
	                        edgeFlag = SwipeBackLayout.EDGE_ALL;
	                }
	                mSwipeBackLayout.setEdgeTrackingEnabled(edgeFlag);
	                saveTrackingMode(edgeFlag);
	            }
	        });
	        mSwipeBackLayout.addSwipeListener(new SwipeBackLayout.SwipeListener() {
	            @Override
	            public void onScrollStateChange(int state, float scrollPercent) {

	            }

	            @Override
	            public void onEdgeTouch(int edgeFlag) {
	                vibrate(VIBRATE_DURATION);
	            }

	            @Override
	            public void onScrollOverThreshold() {
	                vibrate(VIBRATE_DURATION);
	            }
	        });
	    }

	    @Override
	    protected void onResume() {
	        super.onResume();
	        restoreTrackingMode();
	    }

	    private void saveTrackingMode(int flag) {
	        PreferenceUtils.setPrefInt(getApplicationContext(), mKeyTrackingMode, flag);
	    }

	    private void restoreTrackingMode() {
	        int flag = PreferenceUtils.getPrefInt(getApplicationContext(), mKeyTrackingMode,
	                SwipeBackLayout.EDGE_LEFT);
	        mSwipeBackLayout.setEdgeTrackingEnabled(flag);
	        switch (flag) {
	            case SwipeBackLayout.EDGE_LEFT:
	                mTrackingModeGroup.check(R.id.mode_left);
	                break;
	            case SwipeBackLayout.EDGE_RIGHT:
	                mTrackingModeGroup.check(R.id.mode_right);
	                break;
	            case SwipeBackLayout.EDGE_BOTTOM:
	                mTrackingModeGroup.check(R.id.mode_bottom);
	                break;
	            case SwipeBackLayout.EDGE_ALL:
	                mTrackingModeGroup.check(R.id.mode_all);
	                break;
	        }
	    }

	    private void changeActionBarColor() {
	        getActionBar().setBackgroundDrawable(new ColorDrawable(getColors()[mBgIndex]));
	        mBgIndex++;
	        if (mBgIndex >= getColors().length) {
	            mBgIndex = 0;
	        }
	    }

	    private void findViews() {
	        findViewById(R.id.btn_start).setOnClickListener(this);
	        findViewById(R.id.btn_finish).setOnClickListener(this);
	        mTrackingModeGroup = (RadioGroup) findViewById(R.id.tracking_mode);
	    }

	    private int[] getColors() {
	        if (mBgColors == null) {
	            Resources resource = getResources();
	            mBgColors = new int[] {
	                    resource.getColor(R.color.androidColorA),
	                    resource.getColor(R.color.androidColorB),
	                    resource.getColor(R.color.androidColorC),
	                    resource.getColor(R.color.androidColorD),
	                    resource.getColor(R.color.androidColorE),
	            };
	        }
	        return mBgColors;
	    }

	    private void vibrate(long duration) {
	        Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
	        long[] pattern = {
	                0, duration
	        };
	        vibrator.vibrate(pattern, -1);
	    }

	    @Override
	    public void onClick(View v) {
	        switch (v.getId()) {
	            case R.id.btn_start:
	                startActivity(new Intent(SwipeBackDemoActivity.this, SwipeBackDemoActivity.class));
	                break;
	            case R.id.btn_finish:
	                scrollToFinishActivity();
	                break;
	        }
	    }

	    @Override
	    public boolean onCreateOptionsMenu(Menu menu) {
	        getMenuInflater().inflate(R.menu.main, menu);
	        return true;
	    }

	    @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	    	
	    	return super.onOptionsItemSelected(item);
	    	/*
	        switch (item.getItemId()) {
	            case R.id.action_github:
	                Intent intent = new Intent(Intent.ACTION_VIEW);
	                intent.setData(Uri.parse("https://github.com/Issacw0ng/SwipeBackLayout"));
	                startActivity(intent);
	                return true;
	            default:
	                return super.onOptionsItemSelected(item);
	        }
	        */
	    }

}
